package org.rslai.tcedit.web.admin;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.validator.InvalidStateException;
import org.hibernate.validator.InvalidValue;
import org.rslai.tcedit.domain.ExecuteState;
import org.rslai.tcedit.exception.ApplicationException;
import org.rslai.tcedit.util.HttpUtil;
import org.rslai.tcedit.web.core.AbstractMvcController;

/**
 * 测试用例执行状态管理
 * 
 * @author rslai
 * 
 * @spring.bean name="/admin/executeState.jspx"
 */
public class ExecuteStateController extends AbstractMvcController {

	@Override
	public Map<String, Object> getModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String method = request.getMethod();
		Map<String, Object> map = new HashMap<String, Object>();
		ExecuteState executeState = null;

		String action = HttpUtil.getString(request, "action", "").toLowerCase();
		String id = HttpUtil.getString(request, "id", "").toLowerCase();

		try {
			if (action.equals("delete")) { // 删除
				executeState = (ExecuteState) executeStateService.query(ExecuteState.class, id);
				executeStateService.delete(executeState);
				response.sendRedirect("/admin/executeStateList.jspx");
				executeState = null;
			}else if (method.equals("GET")) {
				if  (id.isEmpty()) { // 新增（返回空对象） 
					executeState = new ExecuteState();
				} else { // 查询
					executeState = (ExecuteState) executeStateService.query(ExecuteState.class, id);
				}
			}else if(method.equals("POST")) {
				if (action.equals("create")) { // 新增
					executeState = (ExecuteState) HttpUtil.createFormBean(request, ExecuteState.class);
					executeStateService.create(executeState);
					response.sendRedirect("/admin/executeStateList.jspx");
				} else if (action.equals("update")) { // 修改
					ExecuteState formExecuteState = (ExecuteState) HttpUtil.createFormBean(request, ExecuteState.class);
					executeState = (ExecuteState) executeStateService.query(ExecuteState.class, id);
					executeState.setName(formExecuteState.getName());
					executeState.setDescribing(formExecuteState.getDescribing());
					executeState.setSetInfo(formExecuteState.getSetInfo());
					executeStateService.update(executeState);
					response.sendRedirect("/admin/executeStateList.jspx");
				}
			}
		} catch(Exception e) {
			map.put("error", convertException(e));
		}

		map.put("action", action);
		map.put("executeState", executeState);

		return map;
	}

	@Override
	public String getView(HttpServletRequest request, HttpServletResponse response) {
		return "/admin/executeState.htm";
	}

	private String convertException(Exception e) throws Exception {
		if (e instanceof ApplicationException) {
			return e.getMessage();
		} else if (e instanceof InvalidStateException) {
			StringBuffer sb = new StringBuffer(128);
			InvalidValue[] ivs = ((InvalidStateException) e).getInvalidValues();
			for (InvalidValue iv : ivs)
				sb.append(iv.getMessage()).append("<br/>");
			return sb.toString();
		}
		throw e;
	}

}
